package com.example.wangning.mycarofcountry.splash;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.ImageView;

import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.main.MainActivity;

/**
 * Created by dllo on 16/1/12.
 */
public class SplashActivity extends Activity{
    private ImageView imageView;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        imageView = (ImageView) findViewById(R.id.imageIv);
        handler = new Handler();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.registersheet_in,R.anim.registersheet_out);
                finish();
            }
        });
        if (readWord("splash").equals("0")) {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.registersheet_in,R.anim.registersheet_out);
            finish();

        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 5; i >= 0; i--) {
                        Message msg = new Message();
                        msg.what = 100;
                        msg.arg1 = i;
                        handler.sendMessage(msg);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
            handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    if (msg.what == 100) {
                        saveWord("splash", String.valueOf(msg.arg1));
                        if (readWord("splash").equals("0")) {
                            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                            startActivity(intent);
                            overridePendingTransition(R.anim.registersheet_in,R.anim.registersheet_out);
                            finish();
                        }
                    }
                    return false;
                }
            });
        }
    }
    private void saveWord(String key, String value) {
        if (key.length() > 0 && key != null && value.length() > 0 && value != null) {
            SharedPreferences sp = getSharedPreferences("word", MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, value);
            editor.commit();
        }
    }

    private String readWord(String key) {
        SharedPreferences sp = getSharedPreferences("word", MODE_PRIVATE);
        String s = sp.getString(key, "1");
        return s;
    }

}
